package com.czd.support.controller;

import com.czd.support.common.Constants;
import com.czd.support.model.DataDictionary;
import com.czd.support.model.Manager;
import com.czd.support.model.Navigation;
import com.czd.support.service.DataDictionaryService;
import com.czd.support.service.NavigationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 功能说明：导航管理控制器
 * 作者：liuxing(2014-12-20 22:51)
 */
@Controller
@RequestMapping(value = "/navigation")
public class NavigationController extends BaseController {

    @Autowired
    private NavigationService navigationService;
    @Autowired
    private DataDictionaryService dataDictionaryService;

    /**
     * 层级字典
     * @return
     */
    @ModelAttribute("leaveCodeList")
    protected List<DataDictionary> initQuestionType(){
        return dataDictionaryService.queryListByCode(Constants.NAVIGATION_CODE);
    }

    /**
     * 一级导航
     * @return
     */
    @ModelAttribute("parentNavigations")
    protected List<Navigation> initParentNavigations(){
        return navigationService.queryParentNavigations();
    }

    /**
     * 导航管理列表
     * @return
     */
    @RequestMapping(value = {"", "/", "list"}, method = RequestMethod.GET)
    public String list(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                       Model model) {
        model.addAttribute("paginate", navigationService.queryForPaginate(pageNo));
        return "navigation/list";
    }

    /**
     * 链接详情页
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
    public String details(@PathVariable("id") int id, Model model) {
        Navigation navigation = navigationService.findById(id);
        if (navigation == null) {
            return error(model, "信息不存在", "链接不存在");
        }

        model.addAttribute("navigation", navigation);
        return "navigation/details";
    }

    /**
     * 进入新增
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        model.addAttribute("navigation", new Navigation());
        return "navigation/add";
    }

    /**
     * 保存新增
     * @param name
     * @param link
     * @param tank
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String save(@RequestParam(required = false, value = "parentId") Integer parentId,
                       @RequestParam("name") String name,
                       @RequestParam("link") String link,
                       @RequestParam("tank") int tank,
                       @RequestParam("leaveCode") String leaveCode,
                       HttpServletRequest request, Model model) {
        Manager manager = getSesionUser(request);
        boolean insertFlag = navigationService.insert(parentId, name, link, tank, leaveCode, manager.getUserCode(), manager.getUserCode());
        if (insertFlag) {
            return "redirect:/navigation/list";
        }

        return error(model, "保存失败", "保存失败");
    }

    /**
     * 进入编辑
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String edit(@PathVariable("id") int id, Model model) {
        Navigation navigation = navigationService.findById(id);
        if (navigation == null) {
            return error(model, "信息不存在", "链接不存在");
        }

        model.addAttribute("navigation", navigation);
        return "navigation/edit";
    }

    /**
     * 保存编辑
     * @param id
     * @param name
     * @param link
     * @param tank
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.POST)
    public String save(@PathVariable("id") int id,
                       @RequestParam(required = false, value = "parentId") Integer parentId,
                       @RequestParam("name") String name,
                       @RequestParam("link") String link,
                       @RequestParam("tank") int tank,
                       @RequestParam("leaveCode") String leaveCode,
                       HttpServletRequest request, Model model) {
        Manager manager = getSesionUser(request);
        boolean updateFlag = navigationService.update(id, parentId, name, link, tank, leaveCode, manager.getUserCode());
        if (updateFlag) {
            return "redirect:/navigation/list";
        }

        return error(model, "保存失败", "保存失败");
    }

}
